package com.wj.facedetect.activity.alarm;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wj.facedetect.R;
import com.wj.facedetect.activity.base.BaseActivity;
import com.wj.facedetect.bean.alarm.DealTaskBean;
import com.wj.facedetect.presenter.AlarmPresenter;
import com.wj.facedetect.util.CameraUtil;
import com.wj.facedetect.util.CommonUtils;
import com.wj.facedetect.util.DialogUtil;
import com.wj.facedetect.util.PreferencesUtils;
import com.wj.facedetect.util.ToastUtil;
import com.wj.facedetect.view.IBaseView;
import com.wj.facedetect.widget.MyEditText;

import java.util.HashMap;

public class DealAlarmActivity extends BaseActivity implements IBaseView {
    private TextView tvLeft;
    private RelativeLayout rlClpz;
    private ImageView imageView;
    private MyEditText etBz;
    private Uri faceUri;
    private final int CODE_FACE = 0x001;
    private TextView tvDecs;
    private AlarmPresenter alarmPresenter;
    private String decs;
    private String alarmId;
    private Bitmap bitmap;
    private static final int REQ_DEAL_ALARM = 0x001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deal_alarm);
        alarmPresenter = new AlarmPresenter(this);
        alarmId = getIntent().getStringExtra("alarmId");
        initView();
    }

    private void initView() {
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        rlClpz = (RelativeLayout) findViewById(R.id.rl_xczp);
        rlClpz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                Uri photoUri = CameraUtil.buildPhotoUri(DealAlarmActivity.this);
                CameraUtil.grantPermission(DealAlarmActivity.this, takeIntent, photoUri);
                faceUri = photoUri;
                takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                startActivityForResult(takeIntent, CODE_FACE);
            }
        });
        imageView = (ImageView) findViewById(R.id.iv_photo);
        etBz = (MyEditText) findViewById(R.id.edit_bz);
        tvDecs = (TextView) findViewById(R.id.tv_decs);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CODE_FACE && resultCode == RESULT_OK) {
            if (resultCode == RESULT_OK) {
                bitmap = CameraUtil.handlePhotoByDecodeAndRotateUri(this, 480, 800, faceUri);
                if (bitmap != null) {
                    //压缩图片
                    tvDecs.setVisibility(View.INVISIBLE);
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void commit(View view) {
        //提交上传
        if (bitmap == null) {
            ToastUtil.showMessage("处理图片不能为空");
            return;
        }
        decs = etBz.getText().toString().trim();
        if (TextUtils.isEmpty(decs)) {
            ToastUtil.showMessage("备注不能为空");
            return;
        }
        showProgressDialog();
        requestData();
    }

    public void requestData() {
        HashMap<String, String> params = new HashMap<>();
        params.put("warnId", alarmId);
        params.put("user", PreferencesUtils.getInstance().getSharePreInt(PreferencesUtils.USER_ID) + "");
        params.put("pic", CommonUtils.bitmapToBase64(bitmap));
        params.put("desc", decs);
        alarmPresenter.doRequestPostTaskDeal(params, REQ_DEAL_ALARM, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        alarmPresenter.getModel().cancleRequest(this);
    }

    @Override
    public void onSuccess(int what, Object data, Object carryObj) {
        dismissProgressDialog();
        if (what == REQ_DEAL_ALARM) {
            DealTaskBean dealTaskBean = (DealTaskBean) data;
            if (dealTaskBean != null) {
                if (dealTaskBean.isSuccess()) {
                    DialogUtil.showErrorMsgWithBack(this, "提交上传成功", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            setResult(RESULT_OK);
                            finish();
                        }
                    });
                } else {
                    DialogUtil.showErrorMsg(this, "处理结果上传失败");
                }
            }
        }
    }

    @Override
    public void onError(int what, Exception e, Object carryObj) {
        dismissProgressDialog();
        ToastUtil.showMessage("网络异常");
    }

    @Override
    public void onFail(int what, int errorCode, String errorMsg, Object carryObj) {
        dismissProgressDialog();
    }
}
